//
//  WelcomeViewController.swift
//  sinaweiboFirst
//
//  Created by mac on 16/3/10.
//  Copyright © 2016年 mac. All rights reserved.
//

import UIKit
import SnapKit
import SDWebImage

class WelcomeViewController: UIViewController {

    override func loadView() {
        //自定义根视图
        view = backImageView
    }

    override func viewDidLoad() {
        super.viewDidLoad()
        setupUI()
    }
    
    private func setupUI() {
        view.addSubview(iconImage)
        view.addSubview(welcomeLabel)
        //设置子视图的约束
        iconImage.snp_makeConstraints { (make) -> Void in
            make.centerX.equalTo(view.snp_centerX)
            make.bottom.equalTo(view.snp_bottom).offset(-180)
            //大小 
            make.height.equalTo(90)
            make.width.equalTo(90)
        }
        welcomeLabel.snp_makeConstraints { (make) -> Void in
            make.centerX.equalTo(iconImage.snp_centerX)
            make.top.equalTo(iconImage.snp_bottom).offset(18)
        }
        //最初让文字隐藏
        welcomeLabel.alpha = 0
        
        //设置用户头像 从网络上获取
//        iconImage.sd_setHighlightedImageWithURL(NSURL(string: UserAccount.shareAccount.avatar_large!))
        iconImage.sd_setImageWithURL(UserAccount.shareAccount.headImageURL)
        
        //设置用户头像圆角
        iconImage.layer.cornerRadius = 45
        iconImage.layer.masksToBounds = true
        //圆外环宽度和颜色
        iconImage.layer.borderWidth = 3
        iconImage.layer.borderColor = UIColor.redColor().CGColor
    }
    
    //动画效果不要添加到viewdidload中 界面已经出现时候自动调用 是页面显示之后的生命周期方法 所有子视图的位置已经设置完毕
    override func viewDidAppear(animated: Bool) {
        super.viewDidAppear(animated)
        //动画
        startAnimation()
        
    }

    private  func startAnimation() {
        //需要通过修改约束来实现动画 不能更改frame
        let offset = -UIScreen.mainScreen().bounds.height + 180
        
        self.iconImage.snp_updateConstraints { (make) -> Void in
            make.bottom.equalTo(self.view.snp_bottom).offset(offset)
        }
        
        //usingSpringWithDamping:弹簧系数 0～1 系数越小 弹簧越明显
        //initialSpringVelocity: 重力加速度
        //OC动画的可选项 位移枚举 swift使用数据来表示 [] 表示空数组
        UIView.animateWithDuration(1.5, delay: 0, usingSpringWithDamping: 0.98, initialSpringVelocity: 9.8, options: [], animations: { () -> Void in
            
            //强行刷新页面 layoutIfNeeded  必须在动画闭包中执行
            self.view.layoutIfNeeded()
            
            }) { (_) -> Void in
                //显示欢迎文档
                UIView.animateWithDuration(0.5, animations: { () -> Void in
                    //显示欢迎文字
                    self.welcomeLabel.alpha = 1
                    }, completion: { (_) -> Void in
                        NSNotificationCenter.defaultCenter().postNotificationName(SwitchRootViewController, object: nil)
//                        UIApplication.sharedApplication().keyWindow?.rootViewController = MainViewController()
                })
        }
    }
    
    //懒加载子视图
    //头像
    
    private lazy var backImageView = UIImageView(image: UIImage(named: "ad_background"))
    private lazy var iconImage = UIImageView(image: UIImage(named: "avatar_default_big"))
    //文字标签
    private lazy var welcomeLabel: UILabel = {
        var lab = UILabel()
        lab.text = (UserAccount.shareAccount.name ?? "") + "  欢迎归来"
        lab.textColor = UIColor.darkGrayColor()
        lab.font = UIFont.systemFontOfSize(18)
        lab.sizeToFit()
        return lab
    }()
    
//    var labs: UILabel
//    var labs2 = UILabel()
//    var labs3: UILabel = {}()
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    

   
}
